/**
 * 前台文章相关操作
 * date: 2010-12-9
 */
COMM.lib.load("form");
COMM.lib.load("pager");
(function($){
	
	var Parameter = {
		"rp.articleId" : 0,
		"rp.pageNo" : 1
	};
	
	var PostDiscuss = {
		lastReplyForm : null,
		checkParameter : function(){
			return true;
		},
		post : function(){
			if(!PostDiscuss.checkParameter()){
				alert("输入的参数有错");
				return;
			}
			$.postForm({
				formId : "#discussForm",
				url: "/discuss/add.do",
				success: function(data){
					if(data.result == 0){
						alert("添加失败！");
					} else {
						alert("添加成功！");
						//显示最后一页
						Parameter["rp.pageNo"] = Pager.totalPage;
						Discuss.getData();
					}
				}
			});
		},
		postReply : function(discussId){
			if(!PostDiscuss.checkParameter()){
				alert("输入的参数有错");
				return;
			}
			$.postForm({
				formId : "#discussForm_"+discussId,
				url: "/discuss/reply.do",
				success: function(data){
					if(data.result == 0){
						alert("回复失败！");
					} else {
						alert("回复成功！");
						window.location.href = "#discuss_"+discussId;
						Discuss.getData();
					}
				}
			});
		},
		createReplyForm : function(discussId){
			var form = $("#postDiscuss").clone();
			form.find("#discussForm").attr("id", "discussForm_"+discussId).find("#replyDiscussId").val(discussId);
			form.find("a.btn").eq(0).click(function(){
				PostDiscuss.postReply(discussId);
			});
			form.find("a.btn").eq(1).removeClass("hidden").addClass("show").click(function(){
				PostDiscuss.removeLastReplyForm();
			});
			$("#discuss_"+discussId).after(form);
			this.lastReplyForm = form;
		},
		removeLastReplyForm : function(){
			if(this.lastReplyForm != null) {
				this.lastReplyForm.remove();
				this.lastReplyForm = null;
			}
		}
	};
	
	var Discuss = {

		container : null,
		
		getData : function(){
			$.postJson({
				url: "/discuss/list.do",
				data: Parameter,
				success: function(data){
					Discuss.render(data.pager.pageData);
					if(data.pager.totalPage >= 1){
						Pager.rendrePage(data.pager);
					}
				}
			});
		},
		render : function(data){
			var temp = $("<div></div>");
			for(var i=0;i<data.length;i++){
				temp.append(this.renderItem(data[i], i));
			}
			this.container.empty().append(temp);
		},
		renderItem : function(item, index){
			if(item.isReply == 0){
				return this.createDiscuss(item, index);
			} else {
				return this.createReply(item);
			}
		},

		discussTemplate : "<li id='discuss_#id#'>" +
								"<img src='#avatar#' class='comment-avatar'/>" +
								"<div class='comment-body'>" +
									"<div class='info'>#user#<span><a class='replyBtn' href='javascript:void(0);'>回复</a></span></div>" +
									"<div class='date'>#postDate#</div>" +
									"<div>#content#</div>" +
								"</div>" +
							"</li>",
		createDiscuss : function(item, index){
			var str = this.discussTemplate.replace("#id#",item.id).replace("#postDate#",item.postTime)
				.replace("#avatar#",item.avatar).replace("#content#",item.content);
			if(item.site){
				str = str.replace("#user#", "<a href='"+item.site+"' target='_blank' class='specail-link'>"+item.name+"</a>");
			} else {
				str = str.replace("#user#", item.name);
			}
			var dis = $(str);
//			if(index != 0){
//				dis.css({"border-top": "1px dashed #D6D8D9"});
//			}
//			dis.mouseover(function(){
//				$(this).find("a.replyBtn").show();
//			}).mouseout(function(){
//				$(this).find("a.replyBtn").hide();
//			});
			dis.find("a.replyBtn").click(function(){
				PostDiscuss.removeLastReplyForm();
				PostDiscuss.createReplyForm(item.id);
			});
			return dis;
		},

		replyTemplate : "<li id='discuss_#id#' class='reply'>" +
							"<img src='#avatar#' class='comment-avatar'/>" +
							"<div class='comment-body'>" +
								"<div class='info'>#user#</div>" +
								"<div class='date'>#postDate#</div>" +
								"<div>#content#</div>" +
							"</div>" +
						"</li>",						 
		createReply : function(item){
			var str = this.replyTemplate.replace("#id#",item.id).replace("#postDate#",item.postTime)
					.replace("#avatar#",item.avatar).replace("#content#",item.content);
			if(item.site){
				str = str.replace("#user#", "<a href='#site#' target='_blank' class='specail-link'>"+item.name+"</a>");
			} else {
				str = str.replace("#user#", item.name);
			}
			var dis = $(str);
			return dis;
		}
	};
	
	var Pager = {
		totalPage : 0,
		rendrePage : function(pager){
			this.totalPage = pager.totalPage;
			$("#pagination").pagination({
				totalNum : pager.totalNum,
				totalPage : pager.totalPage,
				currentPage : pager.pageNo,
				displayNum : 6,
				edgeNum : 1,
				callback : function(pageNo){
					Parameter["rp.pageNo"] = pageNo ;
					Discuss.getData();
					window.location.href = "#commentTitle";
				}
			});
		}			
	};
	
	$(document).ready(function(){
		Parameter["rp.articleId"] = $("#articleId").val();
		Discuss.container = $("#discussContent");
		Discuss.getData();
		$("#discussForm a.btn").click(function(){
			PostDiscuss.post();
		});
		//提交文章记数请求
		$.get("/article/click.do?rp.articleId="+$("#articleId").val());
	});
})(jQuery);